let getStorage = (key) => {
  return JSON.parse(localStorage.getItem(key))
}
let setStorage = (key, value) => {
  localStorage.setItem(key, JSON.stringify(value))
}
export default {
  state: {
    list: getStorage('car') || []
  },
  getters: {
    checkedList(state) {
      return state.list.filter(v => v.checked)
    },
    money(state) {
      let money = { CNY: 0, point: 0 };
      state.list.forEach(v => {
        if (v.checked) {
          // money += (v.price * v.sum)
          money.CNY += v.CNY * v.sum;
          money.point += v.point * v.sum;
        }
      })
      return money;
    }
  },
  mutations: {
    SAVECAR(state, list) {
      // console.log(JSON.parse(JSON.stringify(list)));
      // list=list.map(a={

      // });
      state.list = list
      setStorage('car', list)
    }
  },
  actions: {
    addCar({ commit, state, dispatch }, productor) {
      if (!productor || !productor.ID) return;
      let list = [].concat(state.list)
      productor.checked = true
      if (list.length !== 0) {
        productor.sum = 1;
        let i = 999;
        for (i = 0; i < list.length; i++) {
          if (list[i].ID == productor.ID) {
            list[i].sum += productor.sum;
            break;
          }
        }
        if (i >= list.length) list.push(productor)
        commit('SAVECAR', list)
      } else {
        let some = -1;
        list.forEach((v, k) => {
          if (v.id == productor.id) some = k
        })
        if (some >= 0) {
          // list[some].sum++
          dispatch('addNum', some)
        } else {
          productor.sum = 1
          list.push(productor)
        }
        commit('SAVECAR', list)
      }
    },
    addNum({ commit, state }, k) {
      const list = [].concat(state.list)
      list[k].sum++
      commit('SAVECAR', list)
    },
    reduceNum({ commit, state }, k) {
      const list = [].concat(state.list)
      if (list[k].sum == 1) {
        return
      } else {
        list[k].sum--
        commit('SAVECAR', list)
      }

    },
    rmProdutor({ commit, state }, k) {
      const list = [].concat(state.list)
      list.splice(k, 1)
      commit('SAVECAR', list)
    },
    checkProductor({ commit, state }, k) {
      const list = [].concat(state.list)
      list[k].checked = !list[k].checked
      commit('SAVECAR', list)
    },
    // checkAll() {
    //     const list = [].concat(state.list)
    //     for (let v of list) {
    //         v.checked = true;
    //     }
    //     commit('SAVECAR', list)
    // },
    // cancelAll({ commit, state }) {
    //     const list = [].concat(state.list)
    //     for (let v of list) {
    //         v.checked = false;
    //     }
    //     commit('SAVECAR', list)
    // }
  }
}